package com.itheima.web;

import com.itheima.pojo.Contact;
import com.itheima.pojo.PageInfo;
import com.itheima.service.ContactService;
import com.itheima.service.UserService;
import com.itheima.utils.IndexUtils;
import com.itheima.web.base.BaseServlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;

@WebServlet("/contact")
public class ContactServlet extends BaseServlet {
    ContactService service = new ContactService();

    public void findAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Contact> list = service.findAll();
        req.setAttribute("list", list);
        req.getRequestDispatcher("/contact/list.jsp").forward(req, resp);
    }

    public void deleteContactById(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        boolean flag = service.deleteContactById(req.getParameter("id"));
        resp.sendRedirect(req.getContextPath() + "?method=findAll");
    }

    public void addContact(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Contact contact = new Contact();
        contact.setName(req.getParameter("name"));
        contact.setSex(req.getParameter("sex"));
        contact.setAge(Integer.parseInt(req.getParameter("age")));
        contact.setAddress(req.getParameter("address"));
        contact.setQq(req.getParameter("qq"));
        contact.setEmail(req.getParameter("email"));
        service.addContact(contact);
        resp.sendRedirect(req.getContextPath() + "?method=findAll");
    }

    public void findContactById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Contact c = service.findContactById(req.getParameter("id"));
        req.setAttribute("contact", c);
        if (c != null) {
            req.getRequestDispatcher("/contact/update.jsp").forward(req, resp);
        }
    }

    public void updateContactById(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Contact contact = new Contact();
        contact.setId(Integer.parseInt(req.getParameter("id")));
        contact.setName(req.getParameter("name"));
        contact.setSex(req.getParameter("sex"));
        contact.setAge(Integer.parseInt(req.getParameter("age")));
        contact.setAddress(req.getParameter("address"));
        contact.setQq(req.getParameter("qq"));
        contact.setEmail(req.getParameter("email"));
        service.updateContactById(contact);
        resp.sendRedirect(req.getContextPath() + "?method=findAll");
    }

    public void deleteMulti(HttpServletRequest req, HttpServletResponse resp) {
        try {
            String[] ids = req.getParameterMap().get("ids");
            service.deleteMulti(ids);
            resp.sendRedirect(req.getContextPath() + "?method=findAll");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void findByCondition(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HashMap<String, String> map = new HashMap<>();
        map.put("name", req.getParameter("name"));
        map.put("sex", req.getParameter("sex"));
        map.put("qq", req.getParameter("qq"));
        map.put("min", req.getParameter("min"));
        map.put("max", req.getParameter("max"));
        List<Contact> list = service.findByCondition(map);
        req.setAttribute("map", map);
        req.setAttribute("list", list);
        req.getRequestDispatcher("/contact/list.jsp").forward(req, resp);
    }

    public void findByPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int pageSize = 2;
        try {
            pageSize = Integer.parseInt(req.getParameter("pageSize"));
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        int totalCount = service.findTotalCount();
        PageInfo pageInfo = new PageInfo(pageSize, totalCount);
        int currentPage = 1;
        try {
            currentPage = Integer.parseInt(req.getParameter("currentPage"));
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        int totalPage = pageInfo.getTotalPage();
        int startIndex = IndexUtils.start(currentPage, pageSize, totalPage);
        int endIndex = IndexUtils.end(currentPage, startIndex, totalPage);
        req.setAttribute("startIndex", startIndex);
        req.setAttribute("endIndex", endIndex);
        List<Contact> list = service.findByPage(currentPage, pageSize);
        pageInfo.setList(list);
        pageInfo.setCurrentPage(currentPage);
        req.setAttribute("PageInfo", pageInfo);
        req.getRequestDispatcher("/contact/pageList.jsp").forward(req, resp);
    }
}
